package cn.swust.designpatternlearningplatform.service.impl;

import cn.swust.designpatternlearningplatform.dao.ClassInformationDao;
import cn.swust.designpatternlearningplatform.dao.StudentDao;
import cn.swust.designpatternlearningplatform.domain.bo.DailyExerciseTestNumberBo;
import cn.swust.designpatternlearningplatform.domain.dto.StudentAllTypeTopicNumberDto;
import cn.swust.designpatternlearningplatform.domain.dto.StudentPersonInformationDto;
import cn.swust.designpatternlearningplatform.domain.dto.StudentPieChartDataDto;
import cn.swust.designpatternlearningplatform.domain.entity.Student;
import cn.swust.designpatternlearningplatform.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.net.URL;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private ClassInformationDao classInformationDao;
    @Value("${upload_path}")
    public String uploadPath;

    @Override
    @Transactional
    public Student getStudentByNumber(String number) {
        Student student = studentDao.getStudentByNumber(number);
        return student;
    }

    @Transactional
    @Override
    public StudentPersonInformationDto getStudentInformation(String studentNumber) {
        Student student = studentDao.getStudentByNumber(studentNumber);
        String teacherAccount = student.getTeacherAccount();
        String teacherName = classInformationDao.getTeacherName(teacherAccount);
        StudentPersonInformationDto studentPersonInformationDto = new StudentPersonInformationDto();
        studentPersonInformationDto.setName(student.getStudentName());
        studentPersonInformationDto.setNumber(student.getStudentNumber());
        studentPersonInformationDto.setClassAndGrade(student.getClassAndGrade());
        studentPersonInformationDto.setMajor(student.getMajor());
        studentPersonInformationDto.setCollege(student.getCollege());
        studentPersonInformationDto.setHeadPortrait(student.getHeadPortrait());
        studentPersonInformationDto.setReworkMark(student.getReworkMark());
        studentPersonInformationDto.setSchoolTime(student.getSchoolTime());
        studentPersonInformationDto.setPlaceOfClass(student.getPlaceOfClass());
        studentPersonInformationDto.setTeacherName(teacherName);
        return studentPersonInformationDto;
    }

    /**
     * @param studentNumber:
     * @return String
     * @author wang
     * @description 获取学生的头像路径
     * @date 2023/4/17 16:16
     */

    @Override
    @Transactional
    public String getHeadPortrait(String studentNumber) {
        String headPortrait = studentDao.getHeadPortrait(studentNumber);
        return headPortrait;
    }

    /**
     * @param studentNumber:
     * @return Map<Date, Integer>
     * @author wang
     * @description 获取日历图数据返回时间和数量的map函数
     * @date 2023/4/17 16:16
     */

    @Override
    @Transactional
    public Map<LocalDate, Long> QueryNumber(String studentNumber) {
        //获取到数据
        List<DailyExerciseTestNumberBo> list;
        list = studentDao.QueryNumber(studentNumber);
        //进行统计
        Map<LocalDate, Long> collect = list.stream().map(s -> s.getProblemTime().toLocalDate()).collect(Collectors.groupingBy(a -> a, Collectors.counting()));
        return collect;
    }

    @Override
    @Transactional
    public StudentAllTypeTopicNumberDto getAllTypeTopicNumber(String studentNumber) {
        List<String> list = studentDao.getAllTypeTopicNumber(studentNumber);
        Long collect = list.stream().filter(s -> s.equals("shield")).count();
        Long collect1 = list.stream().filter(s -> s.equals("wrong")).count();
        Long collect2 = list.stream().filter(s -> s.equals("collect")).count();
        StudentAllTypeTopicNumberDto studentAllTypeTopicNumberDto = new StudentAllTypeTopicNumberDto();
        studentAllTypeTopicNumberDto.setWrongNumber(collect1);
        studentAllTypeTopicNumberDto.setCollectNumber(collect2);
        studentAllTypeTopicNumberDto.setShieldNumber(collect);
        return studentAllTypeTopicNumberDto;
    }

    /**
     * @param studentNumber:
     * @return StudentPieChartDataDto
     * @author wang
     * description TODO
     * date 2023/4/17 19:19
     */
    @Override
    public StudentPieChartDataDto getPieChartData(String studentNumber) {
        List<Long> studentSelf = studentDao.getSelfNumber(studentNumber);
        List<Long> otherStudentSelf = studentDao.getOtherSelfNumber(studentNumber);
        long myError = studentSelf.stream().filter(s -> s == 0).count();
        long myRight = studentSelf.stream().filter(s -> s == 1).count();
        long otherError = otherStudentSelf.stream().filter(s -> s == 0).count();
        long otherRight = otherStudentSelf.stream().filter(s -> s == 1).count();
        StudentPieChartDataDto studentPieChartDataDto = new StudentPieChartDataDto();
        studentPieChartDataDto.setMyError(myError);
        studentPieChartDataDto.setOtherError(otherError);
        studentPieChartDataDto.setMyRight(myRight);
        studentPieChartDataDto.setOtherRight(otherRight);
        return studentPieChartDataDto;
    }

    /**
     * @param studentNumber:
     * @param url:
     * @return String
     * @author wang
     * description 学生上传头像
     * date 2023/5/21 20:47
     */
    @Override
    public String uploadHead(String studentNumber, String url) {
        int flag = 0;
        try {
            String path = studentDao.getHeadByStudentNumber(studentNumber);
            System.out.println("***************");
            System.out.println(path);
            if (path == null) {
                System.out.println("我进来了");
                studentDao.UploadHead(studentNumber, url);
            } else {
                URL url1 = new URL(path);
                String path1 = url1.getFile();
                System.out.println(path1);
                String substr = path1.substring(8);
                System.out.println(substr);
                //String fileName = path1.substring(path.lastIndexOf("/") + 1);
                String path2 = uploadPath + substr;
                System.out.println("有图片");
                System.out.println(path2);
                File file1 = new File(path2);
                if (file1.delete()) {
                    System.out.println("文件删除成功");
                } else {
                    System.out.println("文件删除失败");
                }
                studentDao.updateHead(studentNumber, url);
            }
        } catch (Exception e) {
            flag = 1;
            throw new RuntimeException(e);
        }
        if (flag == 0) {
            return "上传成功";
        } else {
            return "上传失败";
        }
    }

    @Override
    public String getStudentName(String studentNumber) {

        String studentName = studentDao.getStudentName(studentNumber);
        return studentName;
    }
}
